package gnss

import (
	"fmt"
	"math"
)

/**
  高频度钟差改正(High Rate Clock Correction)
   // 1062(GPS), 1068(GLO)
*/

type IgmasSsrHRClkSat struct {
	Satid uint8
	HRClk float32 //DF390 ±209.7151 m  // Resolution 0.1 mm
}

func (this *IgmasSsrHRClkSat) String() string {
	return fmt.Sprintf("SatId:%d, HRClk:%.4f", this.Satid, this.HRClk)
}

func EncodeIgmasSsrHRClk(head *IgmasSsrCorrectionHead, lst []*IgmasSsrHRClkSat, outBuf []byte, startBitN int) (bitN int) {
	bitN = EncodeIgmasSsrHead(head, outBuf, startBitN, len(lst))

	// 67

	for i := 0; i < len(lst); i++ {
		itm := lst[i]
		SetBitU8(outBuf, bitN, 6, itm.Satid)
		bitN += 6

		SetBitI32(outBuf, bitN, 22, int32(math.Round(float64(itm.HRClk)/1e-4)))
		bitN += 22
	}

	return
}

func DecodeIgmasSsrHRClk(inBuf []byte, startBitN int) (head *IgmasSsrCorrectionHead, lst []*IgmasSsrHRClkSat, bitN int) {
	head = &IgmasSsrCorrectionHead{}
	bitN, n := DecodeIgmasSsrHead(head, inBuf, startBitN)
	lst = make([]*IgmasSsrHRClkSat, n)
	for i := 0; i < len(lst); i++ {
		itm := &IgmasSsrHRClkSat{}
		lst[i] = itm
		itm.Satid = GetBitU8(inBuf, bitN, 6)
		bitN += 6

		itm.HRClk = float32(float64(GetBitI32(inBuf, bitN, 22)) * 1e-4)
		bitN += 22
	}

	return
}
